# -*- coding: utf-8 -*-

import random
import time
import requests
import pytest
from Common.connect_to_device import *
from Common.basepage import BasePage
from Common.global_data import GlobalData
from PageObjects.Main_page import MainPage
from Common.handle_log import do_log
from Common.basepage import BasePage
from Common.handle_log import do_log
from PageLocators.MainPage_locator import MainPageLocator as loc


@pytest.fixture(scope="function")
def handle_IQIYI():
    url = "https://l-rcd.iqiyi.com/apis/mbd/v2/delete.action?auth=1aJBrOltx2n4OAYIp8ZnJjwzuhAK1Q4d4jfiID357OCifAvAo4pVyiSf8qzdm2oZ23wea&ckuid=59d196867a012158614fb57eaef5debe110b&agent_type=655&version=4.3.0501&ua=Xiaomi+Pad+6&network=1&os=13&del_records=1_579296200%2C1_560204700%2C1_7292991076670500%2C1_8562017447924800%2C1_2838564123213700%2C1_604810700&empty=0&req_sn=1716295188913&req_times=0&pps=0&pu=660081951640302&secure_p=GPad&lang=zh_CN&app_lm=cn&qyctxv=1"
    payload = {}
    headers = {
        'qyid': '59d196867a012158614fb57eaef5debe110b',
        'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
        'Accept': '*/*',
        'Host': 'l-rcd.iqiyi.com',
        'Connection': 'keep-alive'
    }

    # response = requests.request("GET", url, headers=headers, data=payload)
    # do_log.info(f"清除爱奇艺播放记录接口响应:{response.json()}")
    # BasePage(device="d_one").start_IQIYI_app()
    BasePage(device="d_one").close_IQIYI_app()
    d_one_device.shell("logcat -c")
    do_log.info("清除logcat缓存")
    if device_type != "A1Back":
        if not d_one(text="极氪影院").exists:
            MainPage(device="d_one").click_home()
            time.sleep(1)

    else:
        count=0
        while not d_one(text="极氪影院").exists and count<6:
            MainPage(device="d_one").click_home()
            count+=1
    yield
    BasePage(device="d_one").close_IQIYI_app()
    time.sleep(1)
def pytest_terminal_summary(terminalreporter, exitstatus, config):
    total = terminalreporter._numcollected
    passed = len(terminalreporter.stats.get('passed', []))
    failed = len(terminalreporter.stats.get('failed', []))
    errors = len(terminalreporter.stats.get('error', []))
    skipped = len(terminalreporter.stats.get('skipped', []))

    success_rate = passed / (passed + failed + errors) * 100 if (passed + failed + errors) > 0 else 0

    duration = time.time() - terminalreporter._sessionstarttime
    hours, remainder = divmod(duration, 3600)
    minutes, seconds = divmod(remainder, 60)
    if hours > 0:
        formatted_duration = f"{int(hours)}h{int(minutes)}m{int(seconds)}s"
    elif minutes > 0:
        formatted_duration = f"{int(minutes)}m{int(seconds)}s"
    else:
        formatted_duration = f"{int(seconds)}s"

    summary = {
        "Total": total,
        "Passed": passed,
        "Failed": failed,
        "Errors": errors,
        "Skipped": skipped,
        "Deselected": total - passed - failed - errors - skipped,
        "SuccessRate": f'{success_rate:.2f}%',
        "TotalTime": formatted_duration
    }
    setattr(GlobalData, "summary", summary)
    do_log.info(f"收集用例执行统计结果为 {summary}")
